/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bookingtickets;

import com.sun.org.apache.xerces.internal.xs.StringList;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author alexxxey
 */
@Stateless
public class BookingTicketsSession implements BookingTicketsSessionRemote, BookingTicketsSessionLocal
{
    /**
     * Create new instance of BookingTicketsSession
     */
    public BookingTicketsSession()
    {
                
    }
        
    @Override
    public List getAllTrips()
    {
        Query query = em.createNamedQuery("Trip.findAll");
        List trips = query.getResultList();                
        return trips;
    }
        
    @Override
    public Trip findTripById(int tripId)
    {
        Query query = em.createNamedQuery("Trip.findById");
        query.setParameter("id", tripId);
        return (Trip)query.getSingleResult();               
    }
        
    @Override
    public List findTripsForDestinations(String destination)
    {        
        Query query = em.createNamedQuery("Destination.findTripByDest");
        query.setParameter("dest", destination);
        List trips = query.getResultList();                
        return trips;
    }
        
    @Override
    public List findTripsForDate(Date date)
    {
        Query query = em.createNamedQuery("Trip.findByTripdate");
        query.setParameter("tripdate",date);
        List trips = query.getResultList();                
        return trips;
    }
            
    @Override
    public void reserveTickets(int tripId, int place, 
            String firstName, String lastName, int pasport)
    {                
        Ticket t = new Ticket();
        t.setTicketPK(new TicketPK(tripId, place));
        t.setFirstname(firstName);
        t.setLastname(lastName);
        t.setPasport(pasport);  
        t.setTrip(findTripById(tripId));
        findTripById(tripId).getTicketList().add(t);        
        
        em.persist(t);             
    }
    
    @Override
    public List reservedTickets(int pasport)
    {
        Query query = em.createNamedQuery("Ticket.findByPasport");
        query.setParameter("pasport", pasport);
        List ticketList = query.getResultList();                
        return ticketList;
    }
        
    @Override
    public void cancelReserve(int place, int tripId)
    {        
        Query query = em.createNamedQuery("Ticket.findByTripAndPalce");
        query.setParameter("place", place);
        query.setParameter("tripid", tripId);
        Ticket t = (Ticket)query.getSingleResult();        
        
        if (t != null)
        {
            findTripById(tripId).getTicketList().remove(t);
            em.remove(t);
        }
    }
    
    @Override
    public List getFreePlacesList(int tripId)
    {
        Trip t = findTripById(tripId);
        return t != null ? t.getFreePlacesList() : null;
    }

    @Override
    public List getDestinationsList(int tripId)
    {        
        Trip t = findTripById(tripId);
        List<String> s = null;
        if (t != null)
        {
            s = new ArrayList<String>(t.getDestinationList().size());
            for (Destination d : t.getDestinationList())
            {
                s.add(d.getDest());
            }
        }
        
        return s;
    }
    
    @PersistenceContext(unitName = "BookingTickets-ejbPU")
    private EntityManager em;                                  
}
